//
//  key.c
//  smart_garbage_case
//
//  Created by fukai on 2018/10.
//  Copyright © 2018年 Cyber01. All rights reserved.
//

#include "stm32f10x.h"	//ST公司v3.5版本使用的库函数头文件
#include "key.h"


KeyMessageNode KeyMessage=
{
	.long_press_mode = 0,	//关闭按键长按模式
	.long_press_check = 0,	//关闭按键长按检测
	.long_press_time = 0,	//初始化按键长按检测时间
};


//简单延时
static void KeyDelay(unsigned int time)
{
	while(--time);
}

//按键初始化函数
void KeyInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);	//使能PORTA时钟,stm32配置外设时必须先使能外设时钟
	//由于PA15占用了JTAG的一个IO，所以要禁止JATAG，从而让PA15用作普通的IO
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag，使能SWD，可以用SWD模式调试
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 			//设置成上拉输入
 	GPIO_Init(GPIOA, &GPIO_InitStructure);					//初始化GPIOC
}

//按键长按检测
//需要用于定时器中断当中
void KeyContinuousCheck(void)
{
	if(KeyMessage.long_press_check == 1)	//开始按键长按检测
	{
		++KeyMessage.long_press_time;
	}
	else
	{
		KeyMessage.long_press_time = 0;
	}
	
	if(KeyMessage.long_press_time == 100)	//已经按下了x的时间
	{
		KeyMessage.long_press_time = 0;
		KeyMessage.long_press_mode = 1;
	}
}

//按键扫描
KeyModeNode KeyScan(void)
{
	static unsigned char key_scan_flag = 1;	//按键扫描标志
	
	if(KeyMessage.long_press_mode == 1)
	{
		key_scan_flag = 1;
	}
	
	if(key_scan_flag &&(KEY0==0 || KEY1==0 || KEY2==0 || KEY3==0 || KEY4==0 || KEY5==0))
	{
		KeyDelay(50);	//消除抖动
		key_scan_flag = 0;	//按键扫描关闭
		//限制特定的按键才能启动长按功能
		if(KEY2==0 || KEY3==0 || KEY4==0 || KEY5==0)
		{
			KeyMessage.long_press_check = 1;	//启动按键长按检测
		}
		if(KeyMessage.long_press_mode == 1)
		{
			KeyDelay(600000);	//防止长按时数据变化太快
			KeyDelay(600000);	//防止长按时数据变化太快
		}
		
		//按键返回键值		
		if(KEY0==0)				return KEY_START;
		else if(KEY1==0)		return KEY_MENU;
		else if(KEY2==0)		return KEY_DIRECTION_UP;
		else if(KEY3==0)		return KEY_ADD;
		else if(KEY4==0)		return KEY_DIRECTION_DOWN;
		else if(KEY5==0)		return KEY_DECREASE;
	}
	else if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1&&KEY5==1)
	{
		key_scan_flag = 1;
		KeyMessage.long_press_check = 0;	//关闭按键长按检测
		KeyMessage.long_press_mode = 0;	//关闭按键长按模式
	}	
	return KEY_NULL;
}

//调试函数      
//Regulated_quantity:被调量
//number：调节量
void DebugC(char *Regulated_quantity,char number) 
{
    if(KeyMessage.KeyValue==KEY_ADD)
      *Regulated_quantity+=number;
    if(KeyMessage.KeyValue==KEY_DECREASE)
      *Regulated_quantity-=number;
}

void DebugI(int *Regulated_quantity,int number) 
{
    if(KeyMessage.KeyValue==KEY_ADD)
      *Regulated_quantity+=number;
    if(KeyMessage.KeyValue==KEY_DECREASE)
      *Regulated_quantity-=number;
}

void DebugD(double *Regulated_quantity,double number) 
{
    if(KeyMessage.KeyValue==KEY_ADD)
      *Regulated_quantity+=number;
    if(KeyMessage.KeyValue==KEY_DECREASE)
      *Regulated_quantity-=number;
}

void DebugF(float *Regulated_quantity,float number) 
{
    if(KeyMessage.KeyValue==KEY_ADD)
      *Regulated_quantity+=number;
    if(KeyMessage.KeyValue==KEY_DECREASE)
      *Regulated_quantity-=number;
}


